package com.xiyi.shouxin.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.xiyi.shouxin.utils.ExceptionUtil;
import com.xiyi.shouxin.utils.FileUtil;
import com.xiyi.shouxin.view.CommonModel;

@CrossOrigin("*")
@RequestMapping("/upload")
@RestController
public class FileController {
    @Autowired
    private Logger logger;
    @Autowired
    private FileUtil fileUtil;

    @PostMapping("/upload_image")
    public CommonModel uploadImage(@RequestParam(value = "image") MultipartFile[] files) {
        SimpleDateFormat format=new SimpleDateFormat("/yyyyMMdd");
        Date cur=new Date();
        try {
            List<String> urls = new ArrayList<>();
            for (MultipartFile file : files) {
                urls.add(fileUtil.upload(file, format.format(cur)));
            }
            return CommonModel.Success(urls);
        } catch (Exception e) {
            logger.error("上传图片异常：", e);
            return ExceptionUtil.GetCommonModel(e);
        }
    }

    @PostMapping("/upload_music")
    public CommonModel uploadMusic(@RequestParam(value = "music") MultipartFile[] files) {
        SimpleDateFormat format=new SimpleDateFormat("/yyyyMMdd");
        Date cur=new Date();
        try {
            List<String> urls = new ArrayList<>();
            for (MultipartFile file : files) {
                urls.add(fileUtil.upload(file, format.format(cur)));
            }
            return CommonModel.Success(urls);
        } catch (Exception e) {
            logger.error("上传音乐异常：", e);
            return ExceptionUtil.GetCommonModel(e);
        }
    }

    @PostMapping("/upload_video")
    public CommonModel uploadVideo(@RequestParam(value = "video") MultipartFile[] files) {
        SimpleDateFormat format=new SimpleDateFormat("/yyyyMMdd");
        Date cur=new Date();
        try {
            List<String> urls = new ArrayList<>();
            for (MultipartFile file : files) {
                urls.add(fileUtil.upload(file, format.format(cur)));
            }
            return CommonModel.Success(urls);
        } catch (Exception e) {
            logger.error("上传视频异常：", e);
            return ExceptionUtil.GetCommonModel(e);
        }
    }
}
